package com.zhufei.filters;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class IsLoginFilter implements Filter {
 
 protected FilterConfig filterConfig;
 
 public void destroy() {
   filterConfig=null;
 }

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   HttpServletRequest req = (HttpServletRequest) request;
   HttpServletResponse res = (HttpServletResponse) response;
   res.setContentType("text/html;charset=utf8"); 
   HttpSession session = req.getSession();
   String isLogin = (String)session.getAttribute("isLogin");
   if (isLogin!=null && isLogin.equals("1")){    //判断session值是否正确
     chain.doFilter(request,response);
   }else{
		PrintWriter   out   =   response.getWriter(); 
		out.write( " <script   language=javascript> alert( '非法登录！ ');window.close(); </script> "); 
		out.flush(); 
		out.close(); 
   }
 }

 public void init(FilterConfig filterConfig) throws ServletException {
   this.filterConfig=filterConfig;
 }

}


